package com.ivt.controller.teacher;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.ivt.core.util.Constants;
import com.ivt.model.Courses;
import com.ivt.model.Teacher;
import com.ivt.model.view.CoursesView;
import com.ivt.model.view.QueryCriteria;
import com.ivt.model.view.Rst;
import com.ivt.service.CoursesService;
import com.ivt.utils.Convert;

@Controller
@RequestMapping("/teacher/courses")
public class CoursesController {

	private final static Logger LOGGER = LoggerFactory
			.getLogger(CoursesController.class);

	@Resource
	private CoursesService coursesService;

	@ModelAttribute
	public void attr(ModelMap view) {
		LOGGER.debug("courses");
		view.addAttribute("moduleName", "courses");
	}

	@RequestMapping("/create")
	public String create(ModelMap view, final HttpServletRequest request) {

		HttpSession session = request.getSession();
		Teacher teacher = (Teacher) session
				.getAttribute(Constants.SESSION_TEACHER_KEY);
		view.addAttribute("teacher", teacher);
		view.addAttribute("moduleAction", "save");
		return "alone:teacher.courses-create";
	}

	@RequestMapping("/qrcode/{courseId}")
	public String qrcode(@PathVariable Long courseId, ModelMap view,
			final HttpServletRequest request) {

		view.addAttribute("courseId", courseId);
		HttpSession session = request.getSession();
		Teacher teacher = (Teacher) session
				.getAttribute(Constants.SESSION_TEACHER_KEY);
		view.addAttribute("teacher", teacher);
		return "alone:teacher.courses-qrcode";
	}

	@RequestMapping("/list")
	public String list(ModelMap view, final HttpServletRequest request) {

		HttpSession session = request.getSession();

		Teacher teacher = (Teacher) session
				.getAttribute(Constants.SESSION_TEACHER_KEY);
		view.addAttribute("teacher", teacher);

		return "alone:teacher.courses-list";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap view) {

		Courses st = coursesService.getCourses(id);

		view.addAttribute("row", st);
		view.addAttribute("moduleAction", "update");

		return "alone:teacher.courses-modify";
	}

	@RequestMapping("/save")
	@ResponseBody
	public Rst save(Courses courses) {

		Rst rst = new Rst();
		courses = coursesService.create(courses);
		if (0 < courses.getId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/pagination")
	@ResponseBody
	public Rst pagination(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		List<Courses> list = coursesService.queryByCriteria(queryCriteria);

		List<CoursesView> views = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (Courses courses : list) {
				views.add(Convert.toCoursesView(courses));
			}
		}
		rst.setCode(Constants.SUCCESS);
		rst.setData(views);
		return rst;
	}

	@RequestMapping("/pages")
	@ResponseBody
	public Rst pages(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		long count = coursesService.queryCount(queryCriteria);
		rst.setCode(Constants.SUCCESS);
		rst.setData(count);
		return rst;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Rst update(Courses courses) {
		Rst rst = new Rst();
		courses = coursesService.update(courses);
		if (0 < courses.getId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Rst update(Long[] ids) {
		Rst rst = new Rst();
		int c = coursesService.deletes(ids);
		rst.setCode(Constants.SUCCESS);
		rst.setMessage("操作成功");
		rst.setData(c);
		return rst;
	}
}
